package com.jakewharton.rxbinding4.component;

import static com.jakewharton.rxbinding4.internal.Preconditions.checkNotNull;

import io.reactivex.rxjava3.annotations.CheckReturnValue;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import ohos.agp.components.RadioContainer;

/**
 * Rx radio container
 */
public final class RxRadioContainer {
    /**
     * Create an observable of the checked view ID changes in {@code view}.
     * <p>
     * <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
     * to free this reference.
     * <p>
     * <em>Note:</em> A value will be emitted immediately on subscribe.
     *
     * @param view view
     * @return the observable
     */
    @CheckReturnValue
    @NonNull
    public static Observable<RadioContainerCheckedChangedEvent> markChanged(@NonNull RadioContainer view) {
        checkNotNull(view, "view == null");
        return new RadioContainerMarkChangedObservable(view);
    }

    /**
     * An action which sets the checked child of {@code view} with ID. Passing {@code -1} will clear
     * any checked view.
     * <p>
     * <em>Warning:</em> The created observable keeps a strong reference to {@code view}. Unsubscribe
     * to free this reference.
     *
     * @param view view
     * @return the consumer
     */
    @CheckReturnValue
    @NonNull
    public static Consumer<? super Integer> marked(@NonNull RadioContainer view) {
        checkNotNull(view, "view == null");
        return value -> {
            if (value == -1) {
                view.cancelMarks();
            } else {
                view.mark(value);
            }
        };
    }

    private RxRadioContainer() {
        throw new AssertionError("No instances.");
    }
}
